package com.ra.landlord;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;

import org.hibernate.jdbc.ReturningWork;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.espirit.eap.manager.SystemLogService;
import com.espirit.eap.manager.TaskSchedulerService;
import com.googlecode.cswish.annotation.Protected;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class LandlordPurchaseInfoService {

	@Resource
	private LandlordUserService landlordUserService;
	
	@Resource
	private GenericService genericService;
	
	@Resource
	private SystemLogService systemLogService;
	
	@Resource
	private TaskSchedulerService taskSchedulerService;
	
	@Protected
	@Async
	public void initLandlordUserPurchaseInfo(LandlordUser landlordUser) {
		LandlordPurchaseInfo landlordPurchaseInfo = new LandlordPurchaseInfo();
		landlordPurchaseInfo.setLandlordUser(landlordUser);
		landlordPurchaseInfo.setType(LandlordPurchaseInfo.TYPE_MESSAGE);
		landlordPurchaseInfo.setCount(4);
		genericService.merge(landlordPurchaseInfo);
		
		landlordPurchaseInfo = new LandlordPurchaseInfo();
		landlordPurchaseInfo.setLandlordUser(landlordUser);
		landlordPurchaseInfo.setType(LandlordPurchaseInfo.TYPE_ADMIN);
		landlordPurchaseInfo.setCount(0);
		genericService.merge(landlordPurchaseInfo);
	}
	
	@Protected
	public int getMessageCount(LandlordUser landlordUser) {
		if (landlordUser == null) {
			return 0;
		}
		
		LandlordPurchaseInfo example = new LandlordPurchaseInfo();
		example.setLandlordUser(landlordUser);
		LandlordPurchaseInfo landlordStat = genericService.searchAll(example).getData(0);
		Integer count = landlordStat == null ? null : landlordStat.getCount();
		return count == null ? 0 : count;
	}
	
	/**
	 * 减少房东的有效短信数
	 * 
	 * @param landlordUser
	 * @param msgCount
	 * @return
	 */
	@Protected
	public boolean reduceMessageCount(LandlordUser landlordUser, int msgCount) {
		// 记录到关键系统日志
		systemLogService.add(LandlordPurchaseInfo.class.getName(), "reduceMessageCount", "landlordUser.id=" + landlordUser.getId() + ",msgCount=" + msgCount);
		
		LandlordPurchaseInfo example = new LandlordPurchaseInfo();
		example.setLandlordUser(landlordUser);
		LandlordPurchaseInfo landlordStat = genericService.searchAll(example).getData(0);
		int count = landlordStat == null || landlordStat.getCount() == null ? 0 : landlordStat.getCount();
		if (count < msgCount) {
			return false;
		} else {
			landlordStat.setCount(count - msgCount);
			genericService.merge(landlordStat);
			return true;
		}
	}
	
	/**
	 * Scheduled: 免费短信数, 每月1号2点发放
	 */
	public void updateFreeMessageCount() {
		genericService.doReturningWork(new ReturningWork<Integer> () {

			@Override
			public Integer execute(Connection connection) throws SQLException {
				String sql = "update ra_landlordstat set user_info_count=4 where type=" + LandlordPurchaseInfo.TYPE_MESSAGE + " and user_info_count<4";
				int count = 0;
				try (Statement stmt = connection.createStatement()) {
					count = stmt.executeUpdate(sql);
				}
				return count;
			}
		});
		taskSchedulerService.updateTaskSchedulerRunTime(this.getClass(), "updateFreeMessageCount");
	}
}